package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"gorm.io/gorm"
)

type ClassifyService struct {
}

// CreateClassify 创建企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) CreateClassify(classify *business.Classify) (err error) {
	err = global.GVA_DB.Create(classify).Error
	return err
}

// DeleteClassify 删除企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) DeleteClassify(classify business.Classify) (err error) {
	db := global.GVA_DB.Model(&business.Classify{}).Where("id = ?", classify.ID)
	if classify.UserId != 0 {
		db = db.Where("user_id = ? ", classify.UserId)
	}
	err = db.Delete(&classify).Error
	return err
}

// DeleteClassifyByIds 批量删除企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) DeleteClassifyByIds(ids request.IdsReq, userId uint) (err error) {
	db := global.GVA_DB.Model(&business.Classify{}).Where("id in (?)", ids)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = global.GVA_DB.Delete(&business.Classify{}).Error
	return err
}

// UpdateClassify 更新企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) UpdateClassify(classify business.Classify) (err error) {
	db := global.GVA_DB.Model(&business.Classify{}).Where("id= ?", classify.ID)
	if classify.UserId != 0 {
		db = db.Where("user_id = ? ", classify.UserId)
	}
	err = db.Update("name", classify.Name).Error
	return err
}

// GetClassify 根据id获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassify(id uint, userId uint) (classify business.Classify, err error) {
	db := global.GVA_DB.Where("id = ? ", id)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = db.First(&classify).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetClassifyByName 根据name获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassifyByName(userId uint, name string) (classify business.Classify, err error) {
	db := global.GVA_DB.Where("user_id = ? and name = ?", userId, name)
	err = db.First(&classify).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetClassifyList 根据idList获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassifyList(idList []uint) (classifyList []business.Classify, err error) {
	db := global.GVA_DB.Where("id in (?) ", idList)
	err = db.Find(&classifyList).Error
	return
}

// GetClassifyListByUserId 根据userId获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassifyListByUserId(userId uint) (classifyList []business.Classify, err error) {
	db := global.GVA_DB.Where("user_id = ? ", userId)
	err = db.Find(&classifyList).Error
	return
}

// GetClassifyInfoList 分页获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassifyInfoList(info businessReq.ClassifySearch) (list []business.Classify, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&business.Classify{})
	var classifys []business.Classify
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&classifys).Error
	return classifys, total, err
}

// GetClassifyInfoList 获取企业分类记录
// Author [piexlmax](https://github.com/piexlmax)
func (classifyService *ClassifyService) GetClassifyListByKey(info businessReq.ClassifySearch) (list []business.Classify, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&business.Classify{})
	var classifys []business.Classify
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&classifys).Error
	return classifys, total, err
}
